﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Krile.Kernel.WindowsEx;

namespace Krile.Forms.MainForm.Controls.XTab
{
    public class XTabTLItem : XTabItem
    {
        public Data.TabData TabData { get; set; }

        public long ScrollOffset { get; set; }

        public XTabTLItem(XTab par, String n)
            : base(par, n, Properties.Resources.comment)
        {
            InitPopupMenu();
            TabData = new Krile.Data.TabData();
            TabData.Parent = this;
            TabData.Name = n;
            TabData.ContainSearchData = null;
            toggleSilent.Checked = TabData.UnNotify;
            toggleLock.Checked = TabData.Locked;
            TabData.InitTabData();
            UpdateIcon();
        }

        public XTabTLItem(XTab par, Data.TabData tdat)
            : base(par, tdat.Name, Properties.Resources.comment)
        {
            InitPopupMenu();
            TabData = tdat;
            TabData.Parent = this;
            toggleSilent.Checked = TabData.UnNotify;
            toggleLock.Checked = TabData.Locked;
            TabData.InitTabData();
            UpdateIcon();
        }

        public static XTabTLItem CreateATLTab(XTab par)
        {
            var ret = new XTabTLItem(par, Core.Config.Appearance.TabAppearance.ATLTabName);
            ret.TabData.ContainSearchData = null;
            return ret;
        }

        ContextMenuStrip cms = null;
        ToolStripMenuItem createQuickTab = null;
        ToolStripMenuItem editThis = null;
        ToolStripMenuItem toggleSilent = null;
        ToolStripMenuItem toggleLock = null;
        private void InitPopupMenu()
        {
            cms = new ContextMenuStrip();

            createQuickTab = new ToolStripMenuItem(Lang.XTab.XTIPop_QTab, Properties.Resources.wand, new EventHandler(CreateQTab));
            cms.Items.Add(createQuickTab);

            cms.Items.Add(new ToolStripSeparator());

            editThis = new ToolStripMenuItem(Lang.XTab.XTIPop_Edit, Properties.Resources.tab_edit, new EventHandler(EditThis));
            cms.Items.Add(editThis);

            toggleLock = new ToolStripMenuItem(Lang.XTab.XTIPop_Lock, Properties.Resources.locked, new EventHandler(ToggleLock));
            cms.Items.Add(toggleLock);

            toggleSilent = new ToolStripMenuItem(Lang.XTab.XTIPop_Silent, null, new EventHandler(ToggleSilent));
            cms.Items.Add(toggleSilent);

            cms.Items.Add(new ToolStripSeparator());

            cms.Items.Add(Lang.XTab.XTIPop_Close, Properties.Resources.tab_delete, new EventHandler(CloseThis));
        }

        internal void CloseThis(object obj, EventArgs e)
        {
            this.CloseThis();
        }

        protected override void CloseThis()
        {
            if (Core.Config.Behavior.ConfirmOnCloseTab)
            {
                bool neverask;
                var r = MessageBoxEx.ShowDialog(
                    Core.FormManager.MainForm,
                    String.Format(Lang.Msg.XTab_ConfirmClose_Context, this.Name),
                    Lang.Msg.XTab_ConfirmClose,
                    Lang.Msg.XTab_ConfirmClose_Caption,
                    MessageBoxEx.MessageBoxExButtons.OKCancel,
                    MessageBoxEx.MessageBoxExIcon.Warning,
                    out neverask);
                Core.Config.Behavior.ConfirmOnCloseTab = !neverask;
                if (r == MessageBoxEx.DialogResultEx.Cancel)
                    return;
            }
            base.CloseThis();
        }

        public override void OnClosing()
        {
            TabData.Dispose();
            base.OnClosing();
        }

        public override void OnMouseClick(object obj, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                cms.Show(parent, e.Location);
            else
                base.OnMouseClick(obj, e);
        }

        public override void OnSelected()
        {
            TabData.RecvNew = false;
            UpdateIcon();
            base.OnSelected();
        }

        private void CreateQTab(object sender, EventArgs e)
        {
            using (var qt = new Krile.Forms.Dialogs.Editor.QuickTab())
            {
                qt.TabData = this.TabData;
                if (qt.ShowDialog() == DialogResult.OK)
                {
                    Core.FormManager.MainForm.PreviousForegroundForm.CreateTab(
                        qt.TabData.Name,
                        qt.TabData.ContainSearchData,
                        qt.TabData.UnNotify,
                        qt.TabData.LinkedServiceIds);
                }
            }
        }

        internal void EditThis(object sender, EventArgs e)
        {
            using (var te = new Dialogs.Editor.TabEditor())
            {
                te.TabData = this.TabData;
                if (te.ShowDialog() == DialogResult.OK)
                {
                    var nt = te.TabData;
                    nt.Locked = this.TabData.Locked;
                    nt.LinkedServiceIds = this.TabData.LinkedServiceIds;
                    nt.UnNotify = this.TabData.UnNotify;
                    nt.Parent = this.TabData.Parent;
                    this.TabData.Dispose();
                    this.TabData = nt;
                    nt.InitTabData();
                    parent.GetParentForm().UpdateTimeline();
                    parent.GetParentForm().TabControl.Refresh();
                }
            }
            Core.SaveConfig(false);
        }

        protected override bool IsLocked
        {
            get
            {
                if (this.TabData != null)
                    return this.TabData.Locked;
                else
                    return false;
            }
        }

        internal void ToggleLock(object sender, EventArgs e)
        {
            this.TabData.Locked = !this.TabData.Locked;
            toggleLock.Checked = this.TabData.Locked;
            UpdateIcon();
            Refresh();
        }

        private void ToggleSilent(object sender, EventArgs e)
        {
            TabData.UnNotify = !TabData.UnNotify;
            toggleSilent.Checked = TabData.UnNotify;
            UpdateIcon();
            Refresh();
        }

        public void UpdateIcon()
        {
            if (this.TabData.RecvNew)
                this.IconImage = Properties.Resources.wordnew;
            else if (TabData.UnNotify)
                this.IconImage = Properties.Resources.comment_unnotify;
            else if (this.TabData.Locked)
                this.IconImage = Properties.Resources.locked;
            else
                this.IconImage = Properties.Resources.comment;
            parent.Refresh();
        }
    }
}
